package com.diankangwo.controller;

import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.FeedbackDAO;
import com.diankangwo.dao.UserDAO;
import com.diankangwo.domain.Feedback;
import com.diankangwo.domain.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 意见反馈控制器
 */
@Controller
@Api(description = "意见反馈")
public class FeedbackController {

    @Autowired
    private UserDAO userDao;

    @Autowired
    private FeedbackDAO fbDao;

    /**
     * 添加意见反馈
     *
     * @param token
     *            token
     * @param content
     *            反馈内容
     * @param fb_pic
     *            反馈图片
     * @return
     */
    @ApiOperation(value = "添加意见反馈", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "content", value = "反馈内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "fb_pic", value = "反馈图片", required = true, dataType = "String")
    })
    @ResponseBody
    @RequestMapping(value = "/fbc/insert", method = RequestMethod.GET)
    public String insert(String token,String content,String fb_pic) {
        UserInfo user = userDao.getUserByToken(token);
        Feedback fb = new Feedback();
        String code = "2";
        String msg = "处理异常！";
        if (user != null) {
            fb.setContent(content);
            fb.setCreateDate(new Date());
            fb.setUid(user.getId());
            fb.setFb_pic(fb_pic);
            if(fbDao.saveOrUpdate(fb) != null){
                code = "1";
                msg = "增加成功！";
            }else{
                msg = "系统异常，增加失败！";
            }
        }else {
            msg = "未查询到用户信息！";
        }
        return resultMsg.mapToJson(code, msg, null);
    }
}
